package com.example.crossborderecommerce.exception;

import com.example.crossborderecommerce.entity.Result;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.naming.AuthenticationException;
import java.sql.SQLException;
import java.sql.SQLIntegrityConstraintViolationException;

@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(ServiceException.class)
    public Result serviceException(ServiceException e) {
        if (e.getCode().equals("401"))
            return new Result(401, e.getMessage(), null);
        else
            return Result.error("未知错误");
    }

    @ExceptionHandler(AuthenticationException.class)
    public Result serviceException(AuthenticationException e) {
        return Result.error(e.getMessage());
    }

//    @ExceptionHandler(SQLException.class)
//    public Result serviceException(SQLException e) {
//        return Result.error("数据库错误");
//    }
}
